<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Predictions extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->model('game');
        $this->load->model('ad');
    }
    // get recomended 3+
    public function getRecomendedOver($arrayOfGames,$recMin){
    	$arrayRecomended= array();
    	foreach ($arrayOfGames as $item) {
            if ($item->getOver()>$recMin){
                array_push($arrayRecomended, $item);
            }
    	}
    	return $arrayRecomended;
    }
    // get recomended 0-2
    public function getRecomendedUnder($arrayOfGames,$recMin){
    	$arrayRecomended= array();
    	foreach ($arrayOfGames as $item) {
            if ($item->getUnder()>$recMin){
    		array_push($arrayRecomended, $item);
            }
    	}
    	return $arrayRecomended;
    }
    // return array of leagues ( grouped )
    public function getUniqueLeagues($arrayOfGames){
    	$leagues=array();
    	foreach ($arrayOfGames as $item){
            if (!(in_array($item->getLeague(), $leagues))){
    		array_push($leagues, $item->getLeague());
            }
       	}
    	return $leagues;
    }
    // filter only games that have no result
    public function filterNotFinished($arrayOfGames){
    	$arrayHelp= array();
    	foreach ($arrayOfGames as $item){
            if ($item->getResult()==' - '){
    		array_push($arrayHelp, $item);
            }
       	}
    	return $arrayHelp;
    }
    // returns top games with percent > $recMin 
    public function getRecomended($arrayOfGames,$recMin){
    	$arrayRecomended= array();
    	foreach ($arrayOfGames as $item) {
            if ($item->getPercentFix1()>$recMin){
    		array_push($arrayRecomended, $item);
            }
            if ($item->getPercentFix2()>$recMin){
            	array_push($arrayRecomended, $item);
            }
            if ($item->getPercentFixX()>$recMin){
    		array_push($arrayRecomended, $item);
            }
    	}
        return $arrayRecomended;
    }        
    // filter games par last date
    public function filterLastDateGames($arrayOfGames){
    	$arrayFiltered= array();
    	$dateOfLast=$arrayOfGames[count($arrayOfGames)-1]->getDateFromPage();
    	foreach ($arrayOfGames as $item) {
            if ($item->getDateFromPage()==$dateOfLast){
    		array_push($arrayFiltered, $item);
            }    		
    	}
    	return $arrayFiltered;
    }
    // index function
    public function index(){
        $arrayOfGames = $this->game->getAllGamesFiltered();
        $clicksor_ad = $this->ad->getAdByName('clicksor');
        $bet365_ad = $this->ad->getAdByName('bet365_2');
        $data['ads'] = array('clicksor'=>$clicksor_ad, 'bet365_2'=>$bet365_ad);
        //$arrayOfGames = $this->filterNotFinished($arrayOfGames);
        $data['games']=$arrayOfGames;
        //$data['leagues']=$this->getUniqueLeagues($arrayOfGames);
        $data['countries']=$this->game->getCountries();
        $data['recomended']=$this->getRecomended($arrayOfGames,75);
        $data['recomendedOver']=$this->getRecomendedOver($arrayOfGames,79);
        $data['recomendedUnder']=$this->getRecomendedUnder($arrayOfGames,75);
        $this->load->helper('url');
        $this->load->view('predictionList',$data);
    }
    public function about(){
        $data['title']="ABOUT";
        $this->load->helper('url');
        $this->load->view('about',$data);
    }
}
